package net.sourceforge.krama.tag.panel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.krama.css.CSSPanel;
import net.sourceforge.krama.css.child.Background;
import net.sourceforge.krama.html.child.HTMLPanel;
import net.sourceforge.krama.listener.TEventListener;
import net.sourceforge.krama.listener.TMouseListener;
import net.sourceforge.krama.tag.widget.AbstractT;
import net.sourceforge.krama.util.TUtil;

import org.apache.commons.lang.StringUtils;


public class TVerticalPanel extends AbstractT {

	private CSSPanel cssPanel = new CSSPanel();
	private HTMLPanel htmlPanel = new HTMLPanel();
	private String html = "";
	private String htmlChild = "";
	private String cssChild = "";
	private String name;
	private String id = "";

	private String parentName;
	
	private String margin;
	private String padding;
	private String height;
	private String width;
	private Background background;
	private String style;

	private String fonction;
	
	private List<TEventListener> listenerList = new ArrayList<TEventListener>();
	
	public TVerticalPanel(String name, String id) {
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
		this.id = id;
		htmlPanel.setId("krama-" + id);
	}

	public void add(AbstractT widget) throws IOException {

		if(parentName == null)
			parentName = TGlobalPanel.name;
		
		widget.setParentName(parentName+
				 " ." + name +" ." + TUtil.VERTICAL_PANEL);
		widget.draw();
		this.htmlChild += TUtil.VerticalEncapsulate(widget.getHtml());
		this.cssChild += widget.getCSS();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
		htmlPanel.setId("krama-" + id);
	}

	@Override
	public void draw() {

		this.html = htmlPanel.starDraw();
		this.html += htmlChild;
		this.html += htmlPanel.endDraw();
	}

	public String getHtml() {
		return html;
	}

	public void setHtml(String html) {
		this.html = html;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public String getCSS() {
		if (TGlobalPanel.name != null && name != null) {
			return cssPanel.getCSS("."+parentName + " ." + name)
					+ ((!StringUtils.isEmpty(cssChild)) 
							? cssPanel.getCSSChild("."+parentName + " ." + name + " ."
							+ TUtil.VERTICAL_PANEL, false) + cssChild : "") ;
		} else
			return "";
	}
	
	public void addTMouseListener(TMouseListener mouseListener, String id)
			throws IllegalArgumentException {

		listenerList.add(mouseListener);
		fonction = mouseListener.onClick();
		fonction += ", 'krama-" + id + "'";
		htmlPanel.addClick(fonction);
	}
	
	public void setMargin(String margin) {
		this.margin = margin;
		cssPanel.setMargin(margin);
	}

	public void setPadding(String padding) {
		this.padding = padding;
		cssPanel.setPadding(padding);
	}

	public void setHeight(String height) {
		this.height = height;
		cssPanel.setHeight(height);
	}

	public void setWidth(String width) {
		this.width = width;
		cssPanel.setWidth(width);
	}

	public void setMargin(int margin) {
		this.margin = String.valueOf(margin)+"px";
		cssPanel.setMargin(this.margin);
	}
	
	public void setPadding(int padding) {
		this.padding = String.valueOf(padding)+"px";
		cssPanel.setPadding(this.padding);
	}

	public void setHeight(int height) {
		this.height = String.valueOf(height)+"px";
		cssPanel.setHeight(this.height);
	}
	
	public void setWidth(int width) {
		this.width = String.valueOf(width)+"px";
		cssPanel.setWidth(this.width);
	}

	public void setBackground(Background background) {
		this.background = background;
		cssPanel.setBackground(background.getBackground());
	}

	public void setStyle(String style) {
		this.style = style;
		cssPanel.setStyle(style);
	}
	
	public String getMargin() {
		return margin;
	}

	public String getPadding() {
		return padding;
	}

	public String getHeight() {
		return height;
	}

	public String getWidth() {
		return width;
	}

	public Background getBackground() {
		return background;
	}

	public String getStyle() {
		return style;
	}
}
